package org.swqsbssddqs.shortlink.project.dao.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.swqsbssddqs.shortlink.project.dao.entity.ShortLinkAccessStatsDO;
import org.swqsbssddqs.shortlink.project.dto.req.ShortLinkStatsReqDTO;

import java.util.List;

public interface ShortLinkAccessStatsMapper extends BaseMapper<ShortLinkAccessStatsDO> {
    @Insert("INSERT INTO `t_link_access_stats` (`full_short_url`,`date`,`pv`,`uv`,`uip`,`hour`,`weekday`,`create_time`,`update_time`,`del_flag`)" +
            " VALUES (#{accessStats.fullShortUrl},#{accessStats.date},#{accessStats.pv},#{accessStats.uv},#{accessStats.uip}," +
            "#{accessStats.hour},#{accessStats.weekday},NOW(), NOW(), 0) " +
            "ON DUPLICATE KEY UPDATE " +
            "pv = pv+#{accessStats.pv}," +
            "uv= uv+#{accessStats.uv}," +
            "uip=uip+#{accessStats.uip}")
    int shortLinkAccessStats(@Param("accessStats") ShortLinkAccessStatsDO entity);

    /**
     * 根据短链接获取指定日期内基础监控数据
     */
    @Select("SELECT " +
            "    tlas.date as `date`, " +
            "    SUM(tlas.pv) AS pv, " +
            "    SUM(tlas.uv) AS uv, " +
            "    SUM(tlas.uip) AS uip " +
            "FROM " +
            "    `t_link` tl INNER JOIN " +
            "    `t_link_access_stats` tlas ON tl.full_short_url = tlas.full_short_url " +
            "WHERE " +
            "    tlas.full_short_url = #{param.fullShortUrl} " +
            "    AND tl.gid = #{param.gid} " +
            "    AND tl.del_flag = '0' " +
            "    AND tl.enable_status = #{param.enableStatus} " +
            "    AND tlas.date BETWEEN #{param.startDate} and #{param.endDate} " +
            "GROUP BY " +
            "    tlas.full_short_url, tl.gid, tlas.date;")
    List<ShortLinkAccessStatsDO> listStatsByShortLink(@Param("param") ShortLinkStatsReqDTO requestParam);
    /**
     * 根据短链接获取指定日期内小时基础监控数据
     */
    @Select("SELECT " +
            "    tlas.hour, " +
            "    SUM(tlas.pv) AS pv " +
            "FROM " +
            "    t_link tl INNER JOIN " +
            "    t_link_access_stats tlas ON tl.full_short_url = tlas.full_short_url " +
            "WHERE " +
            "    tlas.full_short_url = #{param.fullShortUrl} " +
            "    AND tl.gid = #{param.gid} " +
            "    AND tl.del_flag = '0' " +
            "    AND tl.enable_status = #{param.enableStatus} " +
            "    AND tlas.date BETWEEN #{param.startDate} and #{param.endDate} " +
            "GROUP BY " +
            "    tlas.full_short_url, tl.gid, tlas.hour;")
    List<ShortLinkAccessStatsDO> listHourStatsByShortLink(@Param("param")ShortLinkStatsReqDTO requestParam);
    /**
     * 根据短链接获取指定日期内小时基础监控数据
     */
    @Select("SELECT " +
            "    tlas.weekday, " +
            "    SUM(tlas.pv) AS pv " +
            "FROM " +
            "    t_link tl INNER JOIN " +
            "    t_link_access_stats tlas ON tl.full_short_url = tlas.full_short_url " +
            "WHERE " +
            "    tlas.full_short_url = #{param.fullShortUrl} " +
            "    AND tl.gid = #{param.gid} " +
            "    AND tl.del_flag = '0' " +
            "    AND tl.enable_status = #{param.enableStatus} " +
            "    AND tlas.date BETWEEN #{param.startDate} and #{param.endDate} " +
            "GROUP BY " +
            "    tlas.full_short_url, tl.gid, tlas.weekday;")

    List<ShortLinkAccessStatsDO> listWeekdayStatsByShortLink(@Param("param")ShortLinkStatsReqDTO requestParam);
}
